import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
import json

class Testingress:

    @pytest.mark.parametrize('ingressinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/ingress/1_get_ingress_list.yaml'))
    def test_get_ingress_list(self, ingressinfo, get_env_info):
        # 获取ingress列表接口
        baseUrl = get_env_info
        url = baseUrl + ingressinfo['request']['url'].replace('{namespace}','fuyao-oauth')
        method = ingressinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == ingressinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('ingressinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/ingress/2_create_ingress.yaml'))
    def test_create_ingress(self, ingressinfo, get_env_info):
        # 创建ingress接口
        baseUrl = get_env_info
        url = baseUrl + ingressinfo['request']['url'].replace('{namespace}','default')
        method = ingressinfo['request']['method']
        data = ingressinfo['request']['data']
        req = RequestUtil().send_request(method, url,json=data)
        if '不正确' not in ingressinfo['name']:
            ingressname=req.json()['metadata']['name']
            globals()['ingressname']=ingressname
        assert req.status_code == ingressinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('ingressinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/ingress/3_get_specific_ingress.yaml'))
    def test_get_specific_ingress(self, ingressinfo, get_env_info):
        # 获取指定ingress接口
        baseUrl = get_env_info
        url = baseUrl + ingressinfo['request']['url'].replace('{namespace}','default').replace('{ingressname}',globals()['ingressname'])
        method = ingressinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        if '不正确' not in ingressinfo['name']:
            updatereqbody=req.json()
            globals()['updatereqbody']=updatereqbody
        assert req.status_code == ingressinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('ingressinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/ingress/4_update_ingress.yaml'))
    def test_update_ingress(self, ingressinfo, get_env_info):
        # 更新指定ingress接口
        baseUrl = get_env_info
        url = baseUrl + ingressinfo['request']['url'].replace('{namespace}','default').replace('{ingressname}',globals()['ingressname'])
        method = ingressinfo['request']['method']
        ingressinfo['request']['data'] = globals()['updatereqbody']
        data = ingressinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == ingressinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('ingressinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/ingress/5_update_section_ingress.yaml'))
    def test_update_section_ingress(self, ingressinfo, get_env_info):
        # 部分更新指定ingress的接口
        baseUrl = get_env_info
        url = baseUrl + ingressinfo['request']['url'].replace('{namespace}','default').replace('{ingressname}',globals()['ingressname'])
        method = ingressinfo['request']['method']
        data = ingressinfo['request']['data']
        header = ingressinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header,json=data)
        assert req.status_code == ingressinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('ingressinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/ingress/6_delete_specific_ingress.yaml'))
    def test_delete_specific_ingress(self, ingressinfo, get_env_info):
        # 删除ingress接口
        baseUrl = get_env_info
        url = baseUrl + ingressinfo['request']['url'].replace('{namespace}','default').replace('{ingressname}',globals()['ingressname'])
        method = ingressinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        # print(req.content)
        assert req.status_code == ingressinfo['validate'][0]['equals']['status_code']